﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using SCRUM.Core.DAO;
using SCRUM.Core.BusinessObjects;

/// <summary>
/// Handles manipulation with work progress for a user, Create, Edit, Show detail and Delete methods are available
/// </summary>
public partial class WorkProgressDetails : System.Web.UI.UserControl
{
	/// <summary>
	/// Gets or sets the progress.
	/// </summary>
	/// <value>The progress.</value>
	public WorkProgress Progress
	{
		get
		{
			if (ViewState["ViewStateName"] == null)
			{
				return null;
			}
			else
			{
				return (WorkProgress)ViewState["ViewStateName"];
			}
		}
		set
		{
			ViewState["ViewStateName"] = value;
		}
	}

	/// <summary>
	/// Occurs when [action].
	/// </summary>
	public event EventHandler Action;

	/// <summary>
	/// Occurs when [no task assigned].
	/// </summary>
	public event EventHandler NoTaskAssigned;

	protected void Page_Load(object sender, EventArgs e)
	{

	}

	/// <summary>
	/// Binds the drop down list task.
	/// </summary>
	private void BindDropDownListTask()
	{
		List<Task> tasks = DAOTask.GetAllUsersTasks(DAOUser.GetLoggedUser().ID);
		DropDownListTasks.DataSource = tasks;
		DropDownListTasks.DataBind();
		if (tasks.Count == 0)
		{
			OnNoTaskAssigned(this, new EventArgs());
		}
	}

	/// <summary>
	/// Creates the work progress.
	/// </summary>
	/// <param name="sender">The sender.</param>
	/// <param name="e">The <see cref="System.EventArgs"/> instance containing the event data.</param>
	protected void CreateProgress(object sender, EventArgs e)
	{
		DAOWorkProgress.AddProgress(
			Int32.Parse(this.DropDownListTasks.SelectedValue),
			this.DateInputDate.SeledtedDate.Value,
			DAOUser.HoursToStoryPoints(double.Parse(this.ShortTextInputHourCount.Text)),
			this.LongTextInputNote.Text);
		ClearForm();
		this.Visible = false;
		this.Progress = null;
		OnAction(sender, e);
	}

	/// <summary>
	/// Shows the work progress.
	/// </summary>
	/// <param name="workProgress">The work progress.</param>
	public void ShowEditWorkProgress(WorkProgress workProgress)
	{
		this.Progress = workProgress;
		this.rowTaskSelection.Visible = false;
		this.rowTaskInfo.Visible = true;
		this.DateInputDate.Text = workProgress.Date.ToShortDateString();
		//this.DateInputDate.Enabled = false;
		this.ShortTextInputHourCount.Text = DAOUser.StoryPointsToHours(workProgress.StoryPoints).ToString("0.##");
		this.LongTextInputNote.Text = workProgress.Note;
		this.LiteralTaskName.Text = workProgress.TaskName;
		this.ButtonCreate.Visible = false;
		this.ButtonEdit.Visible = true;
		this.Visible = true;
	}

	/// <summary>
	/// Shows the work progress.
	/// </summary>
	public void ShowCreateWorkProgress()
	{
		this.ClearForm();
		this.DateInputDate.SeledtedDate = DateTime.Now;
		this.rowTaskSelection.Visible = true;
		this.BindDropDownListTask();
		this.rowTaskInfo.Visible = false;
		this.ButtonEdit.Visible = false;
		this.ButtonCreate.Visible = true;
		this.Visible = true;
	}

	/// <summary>
	/// Hides the detail.
	/// </summary>
	/// <param name="sender">The sender.</param>
	/// <param name="e">The <see cref="System.EventArgs"/> instance containing the event data.</param>
	protected void HideDetail(object sender, EventArgs e)
	{
		this.Visible = false;
		OnAction(sender, e);
	}

	/// <summary>
	/// Clears the form.
	/// </summary>
	protected void ClearForm()
	{
		this.Progress = null;
		this.DateInputDate.Text = String.Empty;
		this.DateInputDate.Enabled = true;
		this.ShortTextInputHourCount.Text = String.Empty;
		this.LongTextInputNote.Text = String.Empty;
		this.LiteralTaskName.Text = String.Empty;
	}

	/// <summary>
	/// Edits the progress.
	/// </summary>
	/// <param name="sender">The sender.</param>
	/// <param name="e">The <see cref="System.EventArgs"/> instance containing the event data.</param>
	protected void EditProgress(object sender, EventArgs e)
	{
		this.Progress.Date = DateTime.Parse(this.DateInputDate.Text);
		this.Progress.StoryPoints = DAOUser.HoursToStoryPoints(double.Parse(this.ShortTextInputHourCount.Text));
		this.Progress.Note = this.LongTextInputNote.Text;
		DAOWorkProgress.EditProgress(this.Progress);
		this.Visible = false;
		OnAction(sender, e);
	}

	/// <summary>
	/// Called when [action].
	/// </summary>
	/// <param name="sender">The sender.</param>
	/// <param name="e">The <see cref="System.EventArgs"/> instance containing the event data.</param>
	public void OnAction(object sender, EventArgs e)
	{
		if (this.Action != null)
		{
			this.Action(this, e);
		}
	}

	/// <summary>
	/// Called when [no task assigned].
	/// </summary>
	/// <param name="sender">The sender.</param>
	/// <param name="e">The <see cref="System.EventArgs"/> instance containing the event data.</param>
	protected void OnNoTaskAssigned(object sender, EventArgs e)
	{
		if (this.NoTaskAssigned != null)
		{
			this.NoTaskAssigned(this, e);
		}
	}

}
